﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace PhysicsSimulator
{
    class CPig : CGameObject
    {
        private const float MAX_A = (float)(3*Math.PI / 4);
        private Vector2 O = new Vector2(400, -35);
        private const float R = 60.0f;

        CSprite _ropeSprite;
        CSprite _pigSprite;

        Vector2 _position = Vector2.Zero;

        float _angle = 0.0f;
        float _angle_0 = -(float)(Math.PI / 6);
        float _t = 0.0f;

        float _omega = 1.5f;
        float _A = (float)(Math.PI / 3);

        List<CCandy> _candies = new List<CCandy>();
        int _score = 0;
        public int CandyRemain
        {
            get
            {
                return _score;
            } 
        }

        public CPig(SpriteBatch spriteBatch)
            : base(spriteBatch)
        {

        }

        public override void Init(ContentManager contentManager)
        {
            base.Init(contentManager);

            _ropeSprite = new CSprite(contentManager.Load<Texture2D>("Images/CandyGame/rope"));
            _pigSprite = new CSprite(contentManager.Load<Texture2D>("Images/CandyGame/pig"));

            _ropeSprite.Anchor = new Vector2(0.5f, 0.0f);

            _position.X = 400;
            _position.Y = O.Y + _ropeSprite.Height;

        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
            _t += (float)timer.ElapsedTime.TotalSeconds;
            System.Diagnostics.Debug.WriteLine(_t.ToString());
            _angle = _A * (float)(Math.Cos(_omega*_t + _angle_0));

            Vector2 R = new Vector2(O.X, O.Y + _ropeSprite.Height);
            Vector2 OR = Vector2.Subtract(R, O);
            Vector2 OP = RotateByAngle(OR, _angle);
            _position = Vector2.Add(OP, O);

            if (_score > 0)
            {
                if (RandomHelper.Random.Next(0, 6) == 0)
                {
                    _score -= 1;
                    CCandy candy = new CCandy(SpriteBatch);
                    candy.Init(ContentManager);
                    candy.Position = _position;
                    _candies.Add(candy);
                }
            }
            foreach (CCandy candy in _candies)
            {
                candy.Update(timer);
            }
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            base.Draw(timer);
            foreach (CCandy candy in _candies)
            {
                candy.Draw(timer);
            }
            SpriteBatch.Draw(_ropeSprite.Texture,O,null,Color.White,_angle,_ropeSprite.Origin,1,SpriteEffects.None,0);
            SpriteBatch.Draw(_pigSprite.Texture, _position, null, Color.White, _angle, _pigSprite.Origin, 1, SpriteEffects.None, 0.0f);
            
        }

        private Vector2 RotateByAngle(Vector2 v, float angle)
        {
            Vector2 r = Vector2.Zero;
            r.X = (float)( v.X * Math.Cos(angle) - v.Y * Math.Sin(angle));
            r.Y = (float)(v.X * Math.Sin(angle) + v.Y * Math.Cos(angle));
            return r;
        }

        public bool CheckIntersectPoint(Vector2 point)
        {
            Vector2 OR = Vector2.Subtract(point, _position);
            if (OR.Length() <= R)
            {
                return true;
            }
            return false;
        }

        public void Hit(float force)
        {
            _angle_0 = (float)Math.Acos(_angle);
            _t = 2.0f;         
 
            _score = (int)(force*7.5f);
        }

        public void Reset()
        {
            _candies.Clear();
        }
    }
}
